//
//  SlokamController.m
//  TabNav
//
//  Created by Kalyan Mudumbai on 8/28/10.
//  Copyright 2010 GOLDMAN SACHS. All rights reserved.
//

#import "SlokamController.h"
#import "DetailViewController.h"

@implementation SlokamController

@synthesize namesInSlokam;
@synthesize selectedSlokamNumber;
@synthesize nameHolder;

-(void) viewDidLoad {
	nameHolder = [[NameHolder alloc] init];
	[nameHolder loadNames];
	
}

-(void) viewDidUnload {
//	self.namesInSlokam = nil;
	self.nameHolder = nil;
	[super viewDidUnload];
}

-(void) dealloc {
//	[namesInSlokam release];
	[nameHolder release];
	[super dealloc];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSUInteger tempSNo = (selectedSlokamNumber%3);
	
	return [[nameHolder getNameStringsForSlokaNumber:tempSNo] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	NSUInteger tempSNo = (selectedSlokamNumber%3);
	
	NSArray * namesForThisSloka = [nameHolder getNameStringsForSlokaNumber:tempSNo];
	NSArray * sdForThisSloka = [nameHolder getShortDescriptionStringsForSlokaNumber:tempSNo];
	cell.textLabel.text = [namesForThisSloka objectAtIndex:[indexPath row]];
	cell.detailTextLabel.text = [sdForThisSloka objectAtIndex:[indexPath row]];
	

    return cell;
	
}

#pragma mark -
#pragma mark Table view delegate

// A name is selected in the displayed set of names
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger tempSNo = (selectedSlokamNumber%3);	
	NSDictionary * dataForThisSelectedName = [nameHolder getDataForNameId:[indexPath row] InSloka:tempSNo];
	NSLog(@"Data for the selected name is %@ ",dataForThisSelectedName);
	DetailViewController * dvc = [[DetailViewController alloc] init];
	dvc.nameData = dataForThisSelectedName;
	[self.navigationController pushViewController:dvc animated:YES];
	[dvc release];
}


@end
